package cn.ismartv.viewpagerindicator;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by huaijie on 2015/4/8.
 */
public class IconPagerIndicator extends LinearLayout implements PagerIndicator, View.OnClickListener {
    private static final String TAG = "IconPagerIndicator";
    private ViewPager viewPager;
    private List<IconImageView> icons;

    private int indicatorIconWidth;
    private int indicatorIconheight;

    public IconPagerIndicator(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.IconPagerIndicator);

        indicatorIconWidth = (int) typedArray.getDimension(R.styleable.IconPagerIndicator_indicator_icon_width, 0);
        indicatorIconheight = (int) typedArray.getDimension(R.styleable.IconPagerIndicator_indicator_icon_height, 0);
        typedArray.recycle();

        setHorizontalScrollBarEnabled(false);
    }

    @Override
    public void setViewPager(ViewPager viewPager) {
        if (viewPager != null) {
            if (this.viewPager == viewPager) {
                throw new IllegalStateException("You already set the same  ViewPager");
            } else {
                if (this.viewPager != null) {
                    throw new IllegalStateException("You already set ViewPager");
                } else {
                    this.viewPager = viewPager;
                }
            }
        } else {
            throw new IllegalStateException("You can't set null");
        }
        IndicatorFragmentPagerAdapter adapter = (IndicatorFragmentPagerAdapter) viewPager.getAdapter();
        if (adapter == null) {
            throw new IllegalStateException("ViewPager does not have adapter instance.");
        }
        viewPager.addOnPageChangeListener(this);
        initializeView(adapter);
    }


    private void initializeView(IndicatorFragmentPagerAdapter adapter) {
        List<FragmentPager> fragmentPagers = adapter.getFragmentPagers();
        icons = new ArrayList<>();
        for (int i = 0; i < fragmentPagers.size(); i++) {
            IconImageView iconImageView = new IconImageView(getContext());
            iconImageView.setImageResource(fragmentPagers.get(i).getIconResId());
            iconImageView.setId(i);
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(indicatorIconWidth, indicatorIconheight);
            layoutParams.gravity = Gravity.CENTER;
            layoutParams.weight = 1;
            iconImageView.setOnClickListener(this);
            icons.add(iconImageView);
            addView(iconImageView, layoutParams);
        }
        icons.get(0).setSelect(true);
        requestLayout();

    }

    @Override
    public void setCurrentItem(int item) {
        if (viewPager == null) {
            throw new IllegalStateException("ViewPager has not been bound.");
        }
        viewPager.setCurrentItem(item);
    }


    @Override
    public void onPageScrolled(int i, float v, int i1) {

    }

    @Override
    public void onPageSelected(int position) {
        for (int i = 0; i < icons.size(); i++) {
            if (position == i) {
                icons.get(i).setSelect(true);
            } else {
                icons.get(i).setSelect(false);
            }
        }
    }

    @Override
    public void onPageScrollStateChanged(int i) {

    }

    @Override
    public void onClick(View view) {
        int position = view.getId();
        setCurrentItem(position);
//        for (int i = 0; i < icons.size(); i++) {
//            if (icons.get(i).getId() == view.getId()) {
//                setCurrentItem(i);
//            }
//        }
    }
}
